#include<workflow/WFFacilities.h>
#include<workflow/WFTaskFactory.h>
#include<workflow/Workflow.h>
#include<workflow/WFHttpServer.h>
#include<signal.h>
#include<iostream>
#include<string>       
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using std::cout;
using std::cerr;
using std::string;

struct seriescontext
{
    string name;
    string passwd;
    protocol::HttpResponse * reponse;
};

static WFFacilities::WaitGroup waitgroup(1);

void handler(int signalnum)
{
    waitgroup.done();
    cout<<"done\n";
}

void readcallback(WFFileIOTask* task)
{
   FileIOArgs *args= task->get_args();

  seriescontext* context = static_cast<seriescontext*>(series_of(task)->get_context());

context->reponse->append_output_body(static_cast<const char*>(args->buf));
}

void process(WFHttpTask * task)
{
    //读取任务
    protocol::HttpRequest* req = task->get_req();
    string method =req->get_method(); 
    struct seriescontext* context = new seriescontext;
    context->reponse=task->get_resp();
    series_of(task)->set_context(context);
    if(method=="GET")
    {
        int fd = open("postform.html",O_RDONLY);
        char * buf = new char[1000]{0};
        WFFileIOTask* file = WFTaskFactory::create_pread_task(fd ,buf,1000,0,readcallback);
        series_of(task)->push_back(file);
    }
    if(method=="POST")
    {
        cerr<<" no find\n";
    }
}
int main()
{
    //服务端要创建一个server

    WFHttpServer server(process);

    //在process中执行相应的if/else
    if(server.start(1234)==0)
    {
        //执行成功
        waitgroup.wait();
        cout<<"finish\n";
    }
    else
    {
        cerr<<" server start failed \n";
        return -1;
    }
    return 0;
}
